package com.znxs.api_management_c.controller;

import com.znxs.api_management_c.common.BaseResponse;
import com.znxs.api_management_c.common.ErrorCode;
import com.znxs.api_management_c.common.ResultUtils;
import com.znxs.api_management_c.exception.BusinessException;
import com.znxs.api_management_c.model.DTO.UserLoginRequest;
import com.znxs.api_management_c.model.DTO.UserRegisterRequest;
import com.znxs.api_management_c.model.entity.UserInfo;
import com.znxs.api_management_c.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
@RequestMapping("/userInfo")
public class UserInfoController {
    @Resource
    private UserInfoService userInfoService;

    /**
     * 用户登录接口
     * @param userLoginRequest 用户登录请求类
     * @param request http请求
     * @return 登录用户信息
     */
    @PostMapping("/login")
    public BaseResponse<UserInfo> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        // 非空判断
        if (userLoginRequest == null) throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        // 数据校验，这只是数据校验，不设计业务逻辑
        String username = userLoginRequest.getUsername();
        String password = userLoginRequest.getPassword();
        if (StringUtils.isAnyBlank(username, password))
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "请求参数内部数据有空值");
        UserInfo userInfo = userInfoService.userLogin(request, username, password);
        return ResultUtils.success(userInfo);
    }

    /**
     * 用户注册
     *
     * @param userRegisterRequest 用户注册请求对象
     * @return 注册用户id
     */
    @PostMapping("/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        // 进行非空判断
        if (userRegisterRequest == null) throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数对象为空");
        // 数据校验，这只是数据校验，不设计业务逻辑
        String username = userRegisterRequest.getUsername();
        String password = userRegisterRequest.getPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        String telephone = userRegisterRequest.getTelephone();
        System.out.println(userRegisterRequest);
        if (StringUtils.isAnyBlank(username, password, checkPassword, telephone))
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "请求参数内部数据有空值");
        long userId = userInfoService.userRegister(username, password, checkPassword);
        return ResultUtils.success(userId);
    }

    /**
     * 退出登录接口
     * @param request http请求
     * @return
     */
    @PostMapping("/exitLogin")
    public BaseResponse userExitLogin(HttpServletRequest request) {
        return ResultUtils.success(userInfoService.exitLogin(request));
    }

    @GetMapping("/test")
    public BaseResponse<String> apiTest(HttpServletRequest request) {
        request.getSession().setAttribute("test", "测试");
        Object test = request.getSession().getAttribute("test");
        System.out.println(test);
        return ResultUtils.success("测试接口功能");
    }


}
